package com.aaa.service.impl;


import com.aaa.entity.ContractRent;
import com.aaa.entity.Result;
import com.aaa.mapper.ContractRentMapper;
import com.aaa.query.ContractQuery;
import com.aaa.service.IContractRentService;
import com.aaa.util.SakuraStringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auto
 * @since 2021-01-29
 */
@Service
public class ContractRentServiceImpl extends ServiceImpl<ContractRentMapper, ContractRent> implements IContractRentService{

    @Autowired
    ContractRentMapper mapper;

    /**
     *  公有租入合同 查询所有
     */

    @Override
    public Result listAllRentPublic() {

        List<ContractRent> contractRents = mapper.listAllRentPublic();

        return new Result(100021,"查询成功",contractRents);
    }

    /**
     *  公有租入合同 修改合同
     * @param  contract
     */

    @Override
    public Result updateRentPublic(ContractRent contract) {

        int i = mapper.updateRent(contract);

        if(i>0){
            return new Result(100023,"修改成功",null);
        }

        return new Result(100024,"修改失败",null);
    }

    /**
     *  公有租入合同 删除合同
     * @param  number
     */

    @Override
    public Result deleteRent(String number) {

        int i = mapper.deleteById(number);

        if(i>0){
            return new Result(100025,"删除成功",null);
        }
        return new Result(100026,"删除失败",null);
    }

    /**
     *  公有租入合同 添加合同
     * @param contract
     */

    @Override
    public Result addRentPublic(ContractRent contract) {

        int i = mapper.addRentPublic(contract);

        if(i>0){
            return new Result(100031,"添加成功",null);
        }
        return new Result(100032,"添加失败",null);
    }

    @Override
    public Result rentPublicQuery(ContractQuery query) {
        // 获取查询的字段
        QueryWrapper<ContractRent> QueryWrapper = new QueryWrapper<>();
        // 获取字段做非空判断  并与数据库字段作比较
        QueryWrapper.like(SakuraStringUtil.isNull(query.getNumber()),"number",query.getNumber());
        // 获取字段做非空判断  并与数据库字段做比较
        QueryWrapper.like(SakuraStringUtil.isNull(query.getLessor()),"lessor",query.getLessor());

        QueryWrapper.like("property_type",2);
        // 获取起始页 以及每页的数据
        Page<ContractRent> page = new Page<>(query.getPage(),query.getCount());
        // 将分页 与 模糊查询 传入
        Page<ContractRent> map = mapper.selectPage(page, QueryWrapper);
        // 返回 获取的结果
        return new Result(100002,"查询成功",map);
    }

    /**
     *  其他租入合同 查询所有
     */

    @Override
    public Result listAllRentElse() {
        List<ContractRent> contractRents = mapper.listAllRentElse();

        return new Result(100022,"查询成功",contractRents);
    }

    /**
     *  其他租入合同 修改合同
     * @param contract
     */

    @Override
    public Result updateRentElse(ContractRent contract) {

        int i = mapper.updateRent(contract);

        if(i>0){
            return new Result(100027,"修改成功",null);
        }
        return new Result(100028,"修改失败",null);
    }



    /**
     *  其他租入合同 添加合同
     * @param contract
     */

    @Override
    public Result addRentElse(ContractRent contract) {

        int i = mapper.addRentElse(contract);

        if(i>0){
            return new Result(100033,"添加成功",null);
        }
        return new Result(100034,"添加失败",null);
    }

    @Override
    public Result rentElseQuery(ContractQuery query) {
        // 获取查询的字段
        QueryWrapper<ContractRent> QueryWrapper = new QueryWrapper<>();
        // 获取字段做非空判断  并与数据库字段作比较
        QueryWrapper.like(SakuraStringUtil.isNull(query.getNumber()),"number",query.getNumber());
        // 获取字段做非空判断  并与数据库字段做比较
        QueryWrapper.like(SakuraStringUtil.isNull(query.getLessor()),"lessor",query.getLessor());

        QueryWrapper.like("property_type",3);
        // 获取起始页 以及每页的数据
        Page<ContractRent> page = new Page<>(query.getPage(),query.getCount());
        // 将分页 与 模糊查询 传入
        Page<ContractRent> map = mapper.selectPage(page, QueryWrapper);
        // 返回 获取的结果
        return new Result(100002,"查询成功",map);
    }




}
